package org.spring.springboot.app.sys.role.service.impl;

import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.spring.springboot.app.base.R;
import org.spring.springboot.app.base.Type;
import org.spring.springboot.app.sys.role.dao.SysRoleDao;
import org.spring.springboot.app.sys.role.domain.po.RoleManageRolePO;
import org.spring.springboot.app.sys.role.domain.po.SysRoleMenuPO;
import org.spring.springboot.app.sys.role.domain.po.SysRolePO;
import org.spring.springboot.app.sys.role.domain.vo.*;
import org.spring.springboot.app.sys.role.service.ISysRoleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class SysRoleServiceImpl implements ISysRoleService {

    private static final Logger LOGGER = LoggerFactory.getLogger(SysRoleServiceImpl.class);

    @Autowired
    private SysRoleDao sysRoleDao;

    @Override
    public List<SysRoleResVO> findAll() {
        return sysRoleDao.findPage(1, Integer.MAX_VALUE);
    }

    @Override
    public SysRoleResVO find(String id) {
        return sysRoleDao.selectByPrimaryKey(id);
    }

    @Override
    public List<SysRoleResVO> findPage(int pageNum, int pageSize) {
        return sysRoleDao.findPage(pageNum, pageSize);
    }

    @Override
    public List<RoleMenuResVO> findRoleMenu(String id) {
        return sysRoleDao.findRoleMenu(id);
    }

    @Override
    public List<SysRoleResVO> findManageRole(String id) {
        return sysRoleDao.findManageRole(id);
    }

    @Override
    public R save(SysRoleReqVO sysRole) {
        SysRolePO po = new SysRolePO();
        R r = new R();
        if (!r.valid(sysRole)) {
            return r;
        }
        BeanUtils.copyProperties(sysRole, po);
        po.preInsert();
        sysRoleDao.insertSelective(po);
        this.insertRoleMenu(po.getId(), sysRole.getMenus());
        return r;
    }

    private void insertRoleMenu(String roleId, List<String> list) {
        if (StringUtils.isBlank(roleId)) {
            return;
        }
        sysRoleDao.deleteRoleMenu(roleId);
        if (list == null) {
            return;
        }
        for (String menuId : list) {
            SysRoleMenuPO sysRoleMenuPO = new SysRoleMenuPO();
            sysRoleMenuPO.setRoleId(roleId);
            sysRoleMenuPO.setMenuId(menuId);
            sysRoleDao.insertRoleMenu(sysRoleMenuPO);
        }
    }

    @Override
    public R saveRoleManage(RoleManageReqVO param) {
        RoleManageRolePO po = new RoleManageRolePO();
        R r = new R();
        if (!r.valid(param)) {
            return r;
        }
        sysRoleDao.deleteManageRole(param.getRoleId());
        if (param.getRoles() == null) {
            return r;
        }
        for (String roleId : param.getRoles()) {
            po.setSourceRoleId(param.getRoleId());
            po.setManageRoleId(roleId);
            sysRoleDao.insertManageRole(po);
        }
        return r;
    }


    @Override
    public R update(SysRoleReqVO sysRole) {
        SysRolePO po = new SysRolePO();
        R r = new R();
        if (StringUtils.isEmpty(sysRole.getId())) {
            r.setType(Type.ParamValidateFail);
            r.addMessage("id", "参数id不能为空");
        }
        if (!r.valid(sysRole)) {
            return r;
        }
        BeanUtils.copyProperties(sysRole, po);
        po.preUpdate();
        sysRoleDao.updateByPrimaryKeySelective(po);
        this.insertRoleMenu(po.getId(), sysRole.getMenus());
        return r;
    }

    @Override
    public R delete(String id) {
        SysRolePO po = new SysRolePO();
        R r = new R();
        if (StringUtils.isBlank(id)) {
            return r.setType(Type.PowerValidateFail);
        }
        po.setId(id);
        po.preDelete();
        sysRoleDao.updateByPrimaryKeySelective(po);
        return r;
    }


}
